package com.lxf.cloudexpose.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.util.text.AES256TextEncryptor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 罗兴发
 * @data 2025/7/17 9:45
 * @description：提供给第三方的请求接口-控制类
 */
@RestController
@CrossOrigin  //跨域
@RequestMapping("/v1")
@Slf4j
public class ExposeController {
    private static String SECRET_KEY = "MD_AES256";//秘钥自己设置

    @PostMapping("/getExposeUser")
    public String getExposeUser(@RequestBody JSONObject json, HttpServletRequest request){
        String keys = json.getString("keys");
        String appId = json.getString("app_id");
        String encryptData = json.getString("encrypt_data");
        String a = keys+appId;
        String encrypt = encrypt(a,SECRET_KEY);
        // 首先通过appId去数据库查询是否存在该用户
        int flag = 1;
        if (flag<0){
            // 返回appId不存在或者不正确
        }else {
            // 验证使用对称加密后的数据对比是否相同
            if (encrypt.equals(encryptData)){
                // 返回用户请求的数据信息

            }else {
                // 返回秘钥不正确等
            }
        }

        return "";
    }

    public static String encrypt(String data,String privateKey) {
        AES256TextEncryptor encryptor = new AES256TextEncryptor();
        encryptor.setPassword(privateKey);
        return encryptor.encrypt(data);
    }
    public static String decrypt(String encryptedData,String privateKey) {
        AES256TextEncryptor encryptor = new AES256TextEncryptor();
        encryptor.setPassword(privateKey);
        return encryptor.decrypt(encryptedData);
    }
}
